/**
 * WTF Engine
 *
 * License... etc.
 **
 * Font Renderer
 **
 * Author: Sean Micklethwaite
 **/

#ifndef __gui_fontH__
#define __gui_fontH__

#include "texture.h"
#include "wtftypes.h"

// For windows...
#ifdef DrawText
#	undef DrawText
#endif

namespace WtfGui
{
	/**
	 * Generic font interface
	 **/
	class IFont: public virtual WtfEngine::IDataObject {
	public:
		typedef enum {
			ALIGN_LEFT, ALIGN_CENTRE, ALIGN_RIGHT
		} tTextAlignment;

		virtual void		DrawText(const WtfEngine::String& sText, const iVector2D& vBounds, tTextAlignment align) = 0;
		virtual iVector2D	GetSize(const WtfEngine::String& sText, float wrapWidth) = 0;

		DATA_INTERFACE(IFont);
	};

	class Font : public virtual IFont, public WtfEngine::TextureImage
	{
	private:
		struct FontChar
		{
			FontChar*	Next;
			int			CharCode;
			int			Width, Height;
			int			U, V;	// Tex coords
			int			Pre, Post;
			int			Y;

			FontChar(int charCode, int w, int h, int u, int v, int pre, int post, int y)
				: CharCode(charCode), Width(w), Height(h), U(u), V(v), Pre(pre),
					Post(post), Y(y) {
				Next = NULL;
			};
		};

		static const int HASHTBL_SIZE = 64;

		FontChar*	mzFontCharTbl[HASHTBL_SIZE];
		float		mfHeight;
		fVector4D	mvColour;

		FontChar*	LookupChar(unsigned charCode) const;
		void		InsertChar(FontChar* pChar);
		
	public:
		explicit Font(const WtfEngine::StringPool::Entry& sName, float fHeight = 16,
			const fVector4D& vColour = fVector4D(0.5, 0.5, 0.5, 1.0));
		virtual ~Font() {};

		/// Draws text
		void		DrawText(const WtfEngine::String& sText, const iVector2D& vBounds, tTextAlignment align);
		/// Gets the bounds of the drawn text
		iVector2D	GetSize(const WtfEngine::String& sText, float wrapWidth);
		
		float	GetLineWidth(const tChar * sText) const;

		/// Load method for data manager's use
		static WtfEngine::IDataObject * Load(const WtfEngine::StringPool::Entry& sPath);
		
		// TODO: report the correct size here, not a guess
		DATA_LOADABLE(Font, sizeof(Font) + (sizeof(FontChar) * 128));
	};
};


#endif
